package com.chixing.fourteen.controller;


import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.chixing.fourteen.Utils.IntegerUtils;
import com.chixing.fourteen.pojo.Customer;
import com.chixing.fourteen.pojo.Message;
import com.chixing.fourteen.service.CustomerOrderService;
import com.chixing.fourteen.service.impl.CustomerOrderServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.Random;

/**
 * @author Y2014188432
 */
@Controller
@RequestMapping("/customerOrder")
public class CustomerOrderController {

    @Autowired
    private CustomerOrderService customerOrderService = new CustomerOrderServiceImpl();

    @GetMapping("/create")
    public String createOrder(HttpServletRequest request, String startPlace, String endPlace, String remake,double length){
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        int id = customer.getCustomerId();
        int result = customerOrderService.createOrder(id, startPlace, endPlace, remake,length);
        return "forward:/customerOrder/findAll?page=1";
    }

    @GetMapping("/findAll")
    public String findAll(HttpServletRequest request, Model model,int page){
        HttpSession session = request.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        int customerId = customer.getCustomerId();
        model.addAttribute("orderList",customerOrderService.findAll(customerId,page));
        model.addAttribute("page",page);
        model.addAttribute("count", IntegerUtils.upGetInt(customerOrderService.getCustomerOrderCount(customerId),5));
        return "customerOrder";
    }

    @GetMapping("/cancel")
    public String cancelOrder(int id){
        customerOrderService.changeStatus(id,5);
        return "forward:/customerOrder/findAll?page=1";
    }

    @GetMapping("message")
    public String getMessage(Model model,int customerOrderId){
        Message message = customerOrderService.getMessage(customerOrderId);
        Date date =  message.getCreateTime();
        model.addAttribute("message",message);
         return "message";
    }



}
